package com.wedata.海康云;

import cn.hutool.json.JSONObject;
import com.disdr.utils.LettuceRedis;
import com.wedata.HttpUtils;

import java.util.concurrent.TimeUnit;

public class BaseService {

    static String url_token = "https://open.hikyun.com/artemis/oauth/token/v2";

    static String accessKey = "16463796219860916";
    static String secretKey = "ZKqsNoJVDDeeLeJABSip";
    static String productCode = "1646638166955231";
    static String projectId = "361013479217840";
    static String orgId = "2164837673736880";

    static String default_group = "2164544156610224";

    public static void main(String[] args) {
        getToken();
    }

    /**
     * 获取token
     * @return
     */
    public static String getToken(){
        JSONObject param = new JSONObject();
        param.set("accessKey",accessKey);
        param.set("secretKey",secretKey);
        param.set("productCode",productCode);
        param.set("projectId",projectId);
        String token = LettuceRedis.get("wedata:gym:hikyun:token");
        if(token!=null){
            return token;
        }
        JSONObject jsonObject = HttpUtils.sendPost(url_token,null,param);
        System.out.println(jsonObject);
        //3月6：{"code":"200","msg":"操作成功","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MDk2NTg0NDd9.DoffkEEXRtv48QVMGj95DQU6jYrA-Xxx8lNunfDMpEQcRBHogs8V9q0O71QtMw0_-RxwTcl3Cp2Efe4jJWXzhQ","expires_in":43200}}
        //3月6：{"code":"200","msg":"操作成功","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MDk2NTg0NjJ9.2AqlrBXBRFOi4cN27Vr-d1Q6_GUsplitGGz688R5QelqmI5XmxswW1WRR-1DxTK3mL0ZtJNf0CGAiJXJJmix3Q","expires_in":43200}}

        //3月7：{"code":"200","msg":"操作成功","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MDk3MzAzNzl9.2-ME-5M7Yml4R-KhXXEM_klccLl_uIEcYlM-thN63jdJbpKrxTq90_pdAPXYmiXuGOvwjhhAPnfDB7PMY3FiBw","expires_in":43200}}

        //6月20：{"msg":"操作成功","code":"200","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MTg4OTIwNTh9.oy0MqAqG0uMTgaT9bxr8nfbJ-p0XfbjdxzKqwsLynwtFhsIcMJovao6KFc1bwSHx76nMKxEvTxW1iAYQB_6Pqg","expires_in":43200}}
        //6月21：{"msg":"操作成功","code":"200","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MTg5NzUwNDZ9.J21T5m8ANMZi4odMeqW8ytagWnFGWueROtwRmDT6MT8y_bCqSjS0MLYJtXV-JCv4A_19K2KSU6BPVJ15OuVmfQ","expires_in":43200}}
        //6月24号：{"msg":"操作成功","code":"200","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MTkyMzU2MzJ9.39Sd4AQhOHRRlEaasyb8NLM6DzDP69CC8co0tYloqhh9hFdKwHyShygwYbLYPIQ8iLtRzdJ70eDlkI0yGjhvkA","expires_in":43200}}
        //6月24号：{"msg":"操作成功","code":"200","data":{"access_token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhY2Nlc3NfdG9rZW4iLCJwYXlsb2FkIjoie1wiYWNjZXNzS2V5XCI6XCIxNjQ2Mzc5NjIxOTg2MDkxNlwiLFwiZXhwaXJlZFwiOjQzMjAwLFwicHJvZHVjdENvZGVcIjpcIjE2NDY2MzgxNjY5NTUyMzFcIixcInByb2plY3RJZFwiOjM2MTAxMzQ3OTIxNzg0MH0iLCJleHAiOjE3MTkyMzU2Nzd9.FGBNHUVZvrIztbSV_SZbHMK1z2JzMWqLrKdb17WsNWT38HD-JNYhD2kFLfaSpAlpmjOgGnbtHdohjmD-OrWLMQ","expires_in":43200}}
        //可以重复调用，每次都是12小时有效期
        JSONObject data = jsonObject.getJSONObject("data");
        String access_token = data.getStr("access_token");
        Long expires_in = data.getLong("expires_in");
        LettuceRedis.setExpire("wedata:gym:hikyun:token", access_token,expires_in-100);
        return access_token;
    }
    /**
     * 获取token
     * @return
     */
    public static String getToken2() {
        JSONObject param = new JSONObject();
        param.set("accessKey", accessKey);
        param.set("secretKey", secretKey);
        param.set("productCode", productCode);
        param.set("projectId", projectId);
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/global/jwt", null, param);
        System.out.println(jsonObject);
        return null;
    }

}
